#include "TelephoneBook.h"
#include "TelephoneInfo.h"

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

struct TelephoneBook
{
    ElementType *array; // 数组指针
    int capacity;       // 数组容量
    int size;           // 数组大小
};

void Interface(Telebook *t)
{
    printf("1.添加联系人\n");
    printf("2.删除联系人\n");
    printf("3.查找联系人\n");
    printf("4.修改联系人\n");
    printf("5.打印联系人信息\n");
    printf("6.退出\n");
}

Telebook *InitTelebook()
{
    Telebook *t = (Telebook *)malloc(sizeof(Telebook));
    if (t == NULL)
    {
        printf("InitTelebook malloc error!\n");
        exit(-1);
    }
    t->size = 0;
    t->capacity = 10;
    t->array = (ElementType *)malloc(sizeof(ElementType) * t->capacity);
    if (t->array == NULL)
    {
        printf("InitTelebook array error!\n");
        exit(-1);
    }
    return t; // 返回结构体指针
}

void InsertTelebook(Telebook *t)
{
    while (1)
    {
        if (t->size == t->capacity)
        {
            t->capacity *= 2; // 可以扩两倍
            t->array = (ElementType *)realloc(t->array, t->capacity * sizeof(ElementType));
            if (t->array == NULL)
            {
                printf("InitTelebook realloc error!\n");
                exit(-1);
            }
        }
        t->array[t->size] = CreateTelinfo();
        if (t->array[t->size] == NULL)
        {
            printf("请重新输入有效数据!\n");
            continue;
        }
        t->size++;
        printf("插入成功！\n");
        printf("按回车键继续添加联系人,按N返回主界面 \n");
        getchar();
        char a = '0';
        scanf("%c", &a);
        if (a == 'N')
        {
            break;
        }
    }
}

ElementType RemoveByIndex(Telebook *t, int index)
{
    ElementType result = t->array[index];
    for (int i = index; i < t->size - 1; i++)
    {
        t->array[i] = t->array[i + 1];
    }
    t->size--;
    return result;
}

void RemoveTelebook(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    bool flag = false;
    printf("请输入要移除的用户名字/电话号码: \n");
    getchar();
    char value[15];
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
            flag = true;
            ClearTelinfo(t->array[i]);
            //free(t->array[i]);
            RemoveByIndex(t, i);
        }
    }
    if (flag)
    {
        printf("删除联系人成功!\n");
    }
    else
    {
        printf("未查询到相关信息!\n");
    }
    getchar();
    getchar();
}

void FindByElement(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    bool flag = false;
    printf("请输入要查找的用户名字/电话号码: \n");
    char value[15];
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
            printf("找到以下联系人信息：\n");
            PrintTelinfo(t->array[i]);
            flag = true;
        }
    }
    if (!flag)
    {
        printf("未查询到相关联系人\n");
    }
    getchar();
    getchar();
}

void ModifyByElement(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    bool flag = false;
    printf("请输入要修改的用户名字/电话号码: \n");
    char value[15];
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
            flag = true;
            ClearTelinfo(t->array[i]);
            //free(t->array[i]);
            t->array[i]=CreateTelinfo();
            break;
        }
    }
    if (!flag)
    {
        printf("未查询到相关信息\n");
    }
    getchar();
    getchar();
}

void PrintTelebook(Telebook *t, void (*print)(ElementType))
{
    printf("联系人信息: \n");
    for (int i = 0; i < t->size; i++)
    {
        print(t->array[i]);
    }
    printf("\n");
    getchar();
    getchar();
}

void ClearTelebook(Telebook *t, void (*clear)(ElementType))
{
    if (t != NULL)
    {
        for (int i = 0; i < t->size; i++)
        {
            clear(t->array[i]);
        }
        free(t->array);
        free(t);
    }
}
